﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Gift.Entity;

namespace Gift.Core
{
   public class blCompany
    {
       /// <summary>
       /// save
       /// </summary>
       /// <param name="theInfo"></param>
       /// <returns></returns>
       public static int Save(InfoCompany theInfo)
       {
           string strSQL = "";
           int iResult;
           InfoCompany inInfo = theInfo;
           SqlParameter[] sqlPara = new SqlParameter[12];
           sqlPara[0] = new SqlParameter("@Id",SqlDbType.Int);
           sqlPara[0].Value = theInfo.Id;
           sqlPara[1] = new SqlParameter("@UserName",SqlDbType.Text,50);
           sqlPara[1].Value = theInfo.Name;
           sqlPara[2] = new SqlParameter("@UserPass",SqlDbType.Text,50);
           sqlPara[2].Value = theInfo.UserPass;
           sqlPara[3] = new SqlParameter("@Name",SqlDbType.Text,200);
           sqlPara[3].Value = theInfo.Name;
           sqlPara[4] = new SqlParameter("@Address",SqlDbType.Text,300);
           sqlPara[4].Value = theInfo.Address;
           sqlPara[5] = new SqlParameter("@Fax",SqlDbType.Text,50);
           sqlPara[5].Value = theInfo.Fax;
           sqlPara[6] = new SqlParameter("@Telephone",SqlDbType.Text,50);
           sqlPara[6].Value = theInfo.Telephone;
           sqlPara[7] = new SqlParameter("@Postcode",SqlDbType.Text,10);
           sqlPara[7].Value = theInfo.Pastcode;
           sqlPara[8] = new SqlParameter("@Email",SqlDbType.Text,200);
           sqlPara[8].Value = theInfo.Email;
           sqlPara[9] = new SqlParameter("@QQ",SqlDbType.Text,50);
           sqlPara[9].Value = theInfo.QQ;
           sqlPara[10] = new SqlParameter("@Linker",SqlDbType.Text,100);
           sqlPara[10].Value = theInfo.Linker;
           sqlPara[11] = new SqlParameter("@CompanyTypeID",SqlDbType.Int);
           sqlPara[11].Value = theInfo.CompanyTypeId;

           if (theInfo.Id > 0)
           {
               strSQL = "UPDATE tbCompany SET UserName = @UserName,UserPass = @UserPass,Name = @Name,Address = @Address,"
                       +" Fax = @Fax,Telephone = @Telephone,Postcode = @Postcode,Email = @Email,QQ = @QQ,Linker = @Linker,CompanyTypeId = @CompanyTypeId"
                       + " WHERE Id = @Id";
           }
           else
           {
               strSQL = "INSERT INTO tbCompany(UserName,UserPass,Name,Address,Fax,Telephone,Postcode,Email,QQ,Linker,CompanyTypeId)"
                       + " VALUES(@UserName,@UserPass,@Name,@Address,@Fax,@Telephone,@Postcode,@Email,@QQ,@Linker,@CompanyTypeId)";
           }
         iResult =  SQLHelper.ExecuteSQL(CommandType.Text, strSQL, sqlPara);
         return iResult;
       }

       public static DataSet GetList(string sName,string sAddress)
       {
           string sSQL;
           //   SqlParameter[] sPara = new SqlParameter[2];
           //sPara[0] = new SqlParameter("@Name",SqlDbType.Text,200);
           //sPara[0].Value = "上海";
           //sPara[1] = new SqlParameter("@Address",SqlDbType.Text,300);
           //sPara[1].Value = "上海";

           sSQL = "SELECT Id,UserName,UserPass,Name,Address,Fax,Telephone,Postcode,Email,QQ,Linker,CompanyTypeId"
                 + " FROM tbCompany "
                + " WHERE Name like '%" + sName + "%' AND Address like '%" + sAddress + "%'";
          return SQLHelper.GetDataSet(CommandType.Text, sSQL, null);
 
       }

       public static int Delete(int id)
       {
           string sSQL;
           SqlParameter[] sPara = new SqlParameter[1];
           sPara[0] = new SqlParameter("@Id", SqlDbType.Int);
           sPara[0].Value = id;

           sSQL = "DELETE FROM tbCompany  WHERE Id = @id";
           return SQLHelper.ExecuteSQL(CommandType.Text, sSQL, sPara);    
       }

       public static int DeleteMany(string sIds)
       {
           string sSQL;
           if (!string.IsNullOrEmpty(sIds))
           {
               sSQL = "DELETE FROM tbCompany  WHERE Id in ( " + sIds + ")";
           }
           else
           {
               sSQL = "DELETE FROM tbCompany";
           }
           return SQLHelper.ExecuteSQL(CommandType.Text, sSQL, null);
       }

       public static InfoCompany GetRecord(int id)
       {
           string sSQL;
           SqlDataReader sDr;
           InfoCompany theCompany;
              SqlParameter[] sPara = new SqlParameter[1];
           sPara[0] = new SqlParameter("@Id",SqlDbType.Int);
           sPara[0].Value = id;

           sSQL = "SELECT Id,UserName,UserPass,Name,Address,Fax,Telephone,Postcode,Email,QQ,Linker,CompanyTypeId"
                 + " FROM tbCompany "
                + " WHERE Id = @id";
           sDr =  SQLHelper.ExecuteSQLReader(CommandType.Text, sSQL, sPara);
           if (sDr.Read())
           {
               theCompany = new InfoCompany();
               theCompany.Id = sDr.IsDBNull(0) ? theCompany.Id : sDr.GetInt32(0);
               theCompany.UserName = sDr.IsDBNull(1) ? string.Empty : sDr.GetString(1);
               theCompany.UserPass = sDr.IsDBNull(2) ? string.Empty : sDr.GetString(2);
               theCompany.Name = sDr.IsDBNull(3) ? string.Empty : sDr.GetString(3);
               theCompany.Address = sDr.IsDBNull(4) ? string.Empty : sDr.GetString(4);
               theCompany.Fax = sDr.IsDBNull(5) ? string.Empty : sDr.GetString(5);
               theCompany.Telephone = sDr.IsDBNull(6) ? string.Empty : sDr.GetString(7);
               theCompany.Pastcode = sDr.IsDBNull(7) ? string.Empty : sDr.GetString(7);
               theCompany.Email = sDr.IsDBNull(8) ? string.Empty : sDr.GetString(8);
               theCompany.QQ = sDr.IsDBNull(9) ? string.Empty : sDr.GetString(9);
               theCompany.Linker = sDr.IsDBNull(10) ? string.Empty : sDr.GetString(10);
               theCompany.CompanyTypeId = sDr.IsDBNull(11) ? theCompany.CompanyTypeId : sDr.GetInt32(11);
               return theCompany;
           }
           else
           {
               return null;
           }
       }
    }
}
